package handler

import (
	"context"
	"gozeroStandalone/internal/logic"
	"gozeroStandalone/internal/types"
	"gozeroStandalone/utils"
	"math"
	"net/http"
	"strconv"

	"github.com/zeromicro/go-zero/rest/httpx"
)

/*
 +----------------------------------------------------------------------
 + Title        : OrderHandler
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2025-10-24
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 订单处理器
 +----------------------------------------------------------------------
*/
type OrderHandler struct {
	BaseHandler
}

func (control *OrderHandler) GenerateOrder(w http.ResponseWriter, r *http.Request) {
	var req types.OrderCreateRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	orderLogic := logic.NewOrderLogicInstance(context.Background(), control.SvcCtx)
	oid, err := orderLogic.GenerateOrder(req)
	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("下单成功"), control.WithData(map[string]interface{}{
		"oid": oid,
	}))
}

func (control *OrderHandler) Pay(w http.ResponseWriter, r *http.Request) {
	var req types.OrderPayRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 调用逻辑层
	orderLogic := logic.NewOrderLogicInstance(context.Background(), control.SvcCtx)
	err := orderLogic.Pay(req)
	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(r, w, control.WithMessage("支付成功"))
}

/**
 * 列表
 *
 * @param int oid --------------------------非必传,Id
 * @param string oids ----------------------非必传,Id,支持多个
 * @param int platform ---------------------非必传,平台
 * @param int uid --------------------------非必传,用户Id
 * @param string uids ----------------------非必传,用户Id支持多个
 * @param string username ------------------非必传,用户名
 * @param int pay_channel ------------------非必传,支付渠道
 * @param string status --------------------非必传,状态支持多个：1,-1
 * @param string startCreateTime -----------非必传,开始创建时间
 * @param string endCreateTime -------------非必传,结束创建时间
 * @param string sortField -----------------非必传,排序字段
 * @param string sortType ------------------非必传,排序方式
 * @param int pageSize ---------------------非必传,条数
 * @param int page -------------------------非必传,页码
 * @return json
 * @author huwl
 */
func (control *OrderHandler) List(w http.ResponseWriter, r *http.Request) {
	var req types.QueryOrderRequest
	if err := httpx.Parse(r, &req); err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	page := req.Page
	pageSize := req.PageSize
	if req.Page <= 0 {
		page = 1
	}
	if req.PageSize <= 0 {
		pageSize = 10
	}

	// 调用逻辑层
	orderLogic := logic.NewOrderLogicInstance(context.Background(), control.SvcCtx)
	orders, total, err := orderLogic.List(req, page, pageSize)
	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	// 计算总页数
	pageTotal := math.Ceil(float64(total) / float64(pageSize))

	control.BaseSuccess(r, w, control.WithData(map[string]interface{}{
		"page":       page,
		"page_size":  pageSize,
		"page_total": pageTotal,
		"orders":     orders,
	}))
}

/**
 * 详情
 *
 * @param int oid --------------------------非必传,Id
 * @return json
 * @author huwl
 */
func (control *OrderHandler) Detail(w http.ResponseWriter, r *http.Request) {
	oid, _ := strconv.Atoi(utils.DefaultQuery(r, "oid", "0"))
	// 调用逻辑层
	orderLogic := logic.NewOrderLogicInstance(context.Background(), control.SvcCtx)
	order, err := orderLogic.Detail(uint64(oid))
	if err != nil {
		control.BaseError(r, w, control.WithMessage(err.Error()))
		return
	}

	control.BaseSuccess(r, w, control.WithData(order))
}
